package com.snailcatmall.www.serversocket.netty.pipeliNenews;

import com.snailcatmall.www.serversocket.pojo.ClientInfo;
import com.snailcatmall.www.serversocket.tools.GlobalStaticClass;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import io.netty.util.ReferenceCountUtil;

import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class NettyFromClientDataMessages extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            ByteBuf byteDat = (ByteBuf)msg;
            String data = byteDat.toString(Charset.forName("GB2312"));
            Iterator<Map.Entry<SocketAddress, ClientInfo>> entryIterator = GlobalStaticClass.channelHandlerContextMap.entrySet().iterator();
            while (entryIterator.hasNext()){
                Map.Entry<SocketAddress, ClientInfo> socketAddressClientInfoEntry = entryIterator.next();
                switch (socketAddressClientInfoEntry.getValue().getIsDbClient()){
                    case 0:noIdentifierController(socketAddressClientInfoEntry.getKey(),socketAddressClientInfoEntry.getValue(),entryIterator,ctx,byteDat,data);break;
                    case 1:break;
                    default:illegalUnauthorizedExit(socketAddressClientInfoEntry.getKey(),socketAddressClientInfoEntry.getValue(),entryIterator,ctx,byteDat,data);break;
                }
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    /**
     * @param key
     * @param value
     * @param entryIterator
     * @param ctx
     * @param byteDat
     * @param data
     * 该方法是无标识客户端入口
     */
    private void noIdentifierController(SocketAddress key, ClientInfo value, Iterator<Map.Entry<SocketAddress, ClientInfo>> entryIterator, ChannelHandlerContext ctx, ByteBuf byteDat, String data) {
        while (entryIterator.hasNext()){
            Map.Entry<SocketAddress, ClientInfo> socketAddressClientInfoEntry = entryIterator.next();
            if(!socketAddressClientInfoEntry.getKey().equals(ctx.channel().localAddress())){
                if(socketAddressClientInfoEntry.getValue().getIsDbClient()==1){
                    EnumerationNumberOneClient(socketAddressClientInfoEntry.getKey(),socketAddressClientInfoEntry.getValue(),socketAddressClientInfoEntry.getValue().getGroup(),entryIterator,ctx,byteDat,data);
                }
            }
        }
    }

    /**
     * @param key
     * @param value
     * @param group
     * @param entryIterator
     * @param ctx
     * @param byteDat
     * @param data
     * 这个方法判断当前客户端 0 寻找 1的客户端里面的组是否相同
     */
    private void EnumerationNumberOneClient(SocketAddress key, ClientInfo value, List<String> group, Iterator<Map.Entry<SocketAddress, ClientInfo>> entryIterator, ChannelHandlerContext ctx, ByteBuf byteDat, String data) {
    }

    /**
     * @param key 套接字
     * @param value 套接字信息
     * @param entryIterator 集合
     * @param ctx 当前消息套接字
     * @param byteDat 字节
     * @param data 字符串
     * 该方法是非法权限强制下线
     */
    private void illegalUnauthorizedExit(SocketAddress key, ClientInfo value, Iterator<Map.Entry<SocketAddress, ClientInfo>> entryIterator,ChannelHandlerContext ctx, ByteBuf byteDat,String data) {
        ctx.channel().close();
        ctx.pipeline().close();
        ctx.close();
        entryIterator.remove();
        System.out.println("当前在线人数: " + GlobalStaticClass.channelHandlerContextMap.size() +" 位");
    }

}
